package com.eleadmin.order.biz.impl;

import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.damon.utils.utils.DateUtil;
import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.order.biz.OceanBiz;
import com.eleadmin.order.entity.Ocean;
import com.eleadmin.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2023-04-24 11:26:09
 */
@Repository
public class OceanBizImpl extends BaseDao implements OceanBiz {

    @Override
    public JsonResult list(PageResult2<T> page, Ocean ocean, User loginUser) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<Ocean> rm = new BeanPropertyRowMapper<Ocean>(Ocean.class);
        StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.customer_id");
        sql.append(",o.customer,o.shipping_company,o.booking_agent_id,o.booking_agent");
        sql.append(",o.box_type,o.voyage,o.vessel,o.bill_of_lading");
        sql.append(",o.port_of_departure,o.port_of_destination,o.sailing_date,o.signing");
        sql.append(",o.annex,o.seal_no,o.box_no");
        sql.append(",o.salesmanId,o.salesman,o.operaterId,o.operater");
        sql.append(",o.create_time,o.create_user,o.update_time,o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(",cc.bh,cc.containbill_id,cc.apply_id ");
        sql.append(",cc.containbill_lading,cc.apply_lading ");
        sql.append(" from chf_ocean o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=o.update_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
        sql.append(" left join chf_contain_ocean_apply cc on cc.ocean_id=o.id ");
        sql.append(" where o.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and o.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(ocean)) {

            if (Utils.isNotEmpty(ocean.getKeywords())) {
                sql.append(" and (instr(o.customer,:keywords)>0 or instr(o.shipping_company,:keywords)>0 or instr(o.booking_agent,:keywords)>0 " +
                        "or instr(o.sailing_date,:keywords)>0 or instr(o.voyage,:keywords)>0 " +
                        "or instr(o.vessel,:keywords)>0 or instr(o.bill_of_lading,:keywords)>0 ) ");
                map.put("keywords", ocean.getKeywords());
            }
            if (Utils.isNotEmpty(ocean.getCustomer())) {
                sql.append(" and instr(o.customer,:customer)>0 ");
                map.put("customer", ocean.getCustomer());
            }
            if (Utils.isNotEmpty(ocean.getBillOfLading())) {
                sql.append(" and instr(o.bill_of_lading,:bill_of_lading)>0 ");
                map.put("bill_of_lading", ocean.getBillOfLading());
            }
            if (Utils.isNotEmpty(ocean.getCustomer())) {
                sql.append(" and instr(o.customer,:customer)>0 ");
                map.put("customer", ocean.getCustomer());
            }
            if (Utils.isNotEmpty(ocean.getCustomer())) {
                sql.append(" and instr(o.customer,:customer)>0 ");
                map.put("customer", ocean.getCustomer());
            }
            if (Utils.isNotEmpty(ocean.getDate1())) {
                sql.append(" and o.sailing_date>=:date1 ");
                map.put("date1", ocean.getDate1() + " 00:00");
            }
            if (Utils.isNotEmpty(ocean.getDate2())) {
                sql.append(" and o.sailing_date<=:date2 ");
                map.put("date2", ocean.getDate2() + " 23:59");
            }
            if (Utils.isNotEmpty(ocean.getSort()) && Utils.isNotEmpty(ocean.getOrder())) {
                sql.append(" order by o." + ocean.getSort() + " " + ocean.getOrder());
            } else {
                sql.append(" order by o.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<Ocean> list(Ocean ocean, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<Ocean> rm = new BeanPropertyRowMapper<Ocean>(Ocean.class);
        StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.customer_id");
        sql.append(",o.customer,o.shipping_company,o.booking_agent_id,o.booking_agent");
        sql.append(",o.box_type,o.voyage,o.vessel,o.bill_of_lading");
        sql.append(",o.port_of_departure,o.port_of_destination,o.sailing_date,o.signing");
        sql.append(",o.annex,o.seal_no,o.box_no");
        sql.append(",o.salesmanId,o.salesman,o.operaterId,o.operater");
        sql.append(",o.create_time,o.create_user,o.update_time,o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_ocean o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
        sql.append(" where o.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and o.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(ocean)) {
            if (Utils.isNotEmpty(ocean.getSort()) && Utils.isNotEmpty(ocean.getOrder())) {
                sql.append(" order by o." + ocean.getSort() + " " + ocean.getOrder());
            } else {
                sql.append(" order by o.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public Ocean getById(String id) {
        RowMapper<Ocean> rm = new BeanPropertyRowMapper<Ocean>(Ocean.class);
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.customer_id");
        sql.append(",o.customer,o.shipping_company,o.booking_agent_id,o.booking_agent");
        sql.append(",o.box_type,o.voyage,o.vessel,o.bill_of_lading");
        sql.append(",o.port_of_departure,o.port_of_destination,o.sailing_date,o.signing");
        sql.append(",o.annex,o.seal_no,o.box_no");
        sql.append(",o.salesmanId,o.salesman,o.operaterId,o.operater");
        sql.append(",o.create_time,o.create_user,o.update_time,o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_ocean o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
        sql.append(" where o.deleted=0 and o.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(Ocean ocean, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("insert into chf_ocean (");
        sql.append("organization_id,customer_id,customer,shipping_company");
        sql.append(",booking_agent_id,booking_agent,box_type,voyage");
        sql.append(",vessel,bill_of_lading,port_of_departure,port_of_destination");
        sql.append(",annex,seal_no,box_no");
        sql.append(",sailing_date,signing,salesmanId,salesman");
        sql.append(",operaterId,operater,create_user,create_time) values ( ");
        sql.append(":organization_id,:customer_id,:customer,:shipping_company");
        sql.append(",:booking_agent_id,:booking_agent,:box_type,:voyage");
        sql.append(",:vessel,:bill_of_lading,:port_of_departure,:port_of_destination");
        sql.append(",:annex,:seal_no,:box_no");
        sql.append(",:sailing_date,:signing,:salesmanId,:salesman");
        sql.append(",:operaterId,:operater,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("customer_id", ocean.getCustomerId() == null ? null : ocean.getCustomerId() + "");
        map.put("customer", ocean.getCustomer());
        map.put("shipping_company", ocean.getShippingCompany());
        map.put("booking_agent_id", ocean.getBookingAgentId() == null ? null : ocean.getBookingAgentId() + "");
        map.put("booking_agent", ocean.getBookingAgent());
        map.put("box_type", ocean.getBoxType());
        map.put("voyage", ocean.getVoyage());
        map.put("vessel", ocean.getVessel());
        map.put("bill_of_lading", ocean.getBillOfLading());
        map.put("port_of_departure", ocean.getPortOfDeparture());
        map.put("port_of_destination", ocean.getPortOfDestination());
        map.put("annex", ocean.getAnnex());
        map.put("seal_no", ocean.getSealNo());
        map.put("box_no", ocean.getBoxNo());
        map.put("sailing_date", ocean.getSailingDate());
        map.put("signing", ocean.getSigning());
        map.put("salesmanId", ocean.getSalesmanid() == null ? null : ocean.getSalesmanid() + "");
        map.put("salesman", ocean.getSalesman());
        map.put("operaterId", ocean.getOperaterid() == null ? null : ocean.getOperaterid() + "");
        map.put("operater", ocean.getOperater());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(Ocean ocean, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("update chf_ocean set ");
        sql.append("customer_id=:customer_id,customer=:customer,shipping_company=:shipping_company ");
        sql.append(",booking_agent_id=:booking_agent_id,booking_agent=:booking_agent,box_type=:box_type ");
        sql.append(",voyage=:voyage,vessel=:vessel,bill_of_lading=:bill_of_lading ");
        sql.append(",port_of_departure=:port_of_departure,port_of_destination=:port_of_destination,sailing_date=:sailing_date ");
        sql.append(",annex=:annex,seal_no=:seal_no,box_no=:box_no ");
        sql.append(",signing=:signing,salesmanId=:salesmanId,salesman=:salesman ");
        sql.append(",operaterId=:operaterId,operater=:operater,update_time=now() ");
        sql.append(",update_user=:update_user where id=:id ");
        map.put("customer_id", ocean.getCustomerId() == null ? null : ocean.getCustomerId() + "");
        map.put("customer", ocean.getCustomer());
        map.put("shipping_company", ocean.getShippingCompany());
        map.put("booking_agent_id", ocean.getBookingAgentId() == null ? null : ocean.getBookingAgentId() + "");
        map.put("booking_agent", ocean.getBookingAgent());
        map.put("box_type", ocean.getBoxType());
        map.put("voyage", ocean.getVoyage());
        map.put("vessel", ocean.getVessel());
        map.put("bill_of_lading", ocean.getBillOfLading());
        map.put("port_of_departure", ocean.getPortOfDeparture());
        map.put("port_of_destination", ocean.getPortOfDestination());
        map.put("sailing_date", ocean.getSailingDate());
        map.put("annex", ocean.getAnnex());
        map.put("seal_no", ocean.getSealNo());
        map.put("box_no", ocean.getBoxNo());
        map.put("signing", ocean.getSigning());
        map.put("salesmanId", ocean.getSalesmanid() == null ? null : ocean.getSalesmanid() + "");
        map.put("salesman", ocean.getSalesman());
        map.put("operaterId", ocean.getOperaterid() == null ? null : ocean.getOperaterid() + "");
        map.put("operater", ocean.getOperater());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", ocean.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if (id.length() > 0) {
            for (String id1 : id.split(",")) {
                map = new HashMap<String, String>();
                if (id1 == null || id1.equals("")) {
                    break;
                }
                StringBuffer sql = new StringBuffer("update chf_ocean set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(), map);
            }
        }
        return count > 0;
    }
}
